package com.jzo2o.orders.base.handler;

import com.jzo2o.common.expcetions.DBException;
import com.jzo2o.orders.base.enums.OrderStatusEnum;
import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.base.model.dto.OrderSnapshotDTO;
import com.jzo2o.orders.base.service.IOrdersCommonService;
import com.jzo2o.statemachine.core.StatusChangeEvent;
import com.jzo2o.statemachine.core.StatusChangeHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @Author yzx
 * @Date 2024/12/17 11:58
 */
@Slf4j
@Component("order_close_dispatching_order")
public class OrderCloseDispatchingHandler implements StatusChangeHandler<OrderSnapshotDTO> {

    @Resource
    private IOrdersCommonService ordersCommonService;
    /**
     * 状态变更处理逻辑
     * @param bizId
     * @param statusChangeEventEnum
     * @param bizSnapshot
     */
    @Override
    public void handler(String bizId, StatusChangeEvent statusChangeEventEnum, OrderSnapshotDTO bizSnapshot) {
        log.info("派单中订单关闭事件，触发状态变更，开始变更：{}", bizId);
        boolean resultForOrder = ordersCommonService.lambdaUpdate()
                .eq(Orders::getId, bizSnapshot.getId())
                .set(Orders::getOrdersStatus, OrderStatusEnum.CLOSED.getStatus())
                .set(Orders::getRefundStatus, bizSnapshot.getRefundStatus())
                .set(Orders::getRefundNo, bizSnapshot.getRefundNo())
                .set(Orders::getRefundId, bizSnapshot.getThirdRefundOrderId())
                .update();
        if (!resultForOrder){
            throw new DBException("[退款操作]更新订单表失败");
        }
        log.info("派单中订单关闭事件，触发状态变更，变更完毕：{}",bizId);
    }
}
